package com.sangu.dianxiu.ui;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.sangu.dianxiu.ICallBack;
import com.sangu.dianxiu.R;
import com.sangu.dianxiu.adapter.RepairerCommentLvAdapter;
import com.sangu.dianxiu.bean.repairer.MasterDetailEntity;
import com.sangu.dianxiu.bean.repairer.RepairerCommentEntity;
import com.sangu.dianxiu.utils.MasterManager;
import com.sangu.dianxiu.utils.RepairOrderManager;

import java.util.ArrayList;
import java.util.HashMap;

public class RepairerDetailActivity extends BaseActivity {

    @ViewInject(R.id.tv_activity_repairer_detail_name)          //使用xutils的注解功能，这样就不用findviewbyid了
    private TextView repairerName;
    @ViewInject(R.id.tv_activity_repairer_detail_name)
    private TextView repairerSkill;
    @ViewInject(R.id.iv_activity_repairer_detail_phone)
    private TextView repairerPhone;
    @ViewInject(R.id.iv_activity_repairer_detail_star1)
    private ImageView ivStar1;
    @ViewInject(R.id.iv_activity_repairer_detail_star2)
    private ImageView ivStar2;
    @ViewInject(R.id.iv_activity_repairer_detail_star3)
    private ImageView ivStar3;
    @ViewInject(R.id.iv_activity_repairer_detail_star4)
    private ImageView ivStar4;
    @ViewInject(R.id.iv_activity_repairer_detail_star5)
    private ImageView ivStar5;
    @ViewInject(R.id.tv_activity_repairer_detail_times)
    private TextView tvServeTimes;
    @ViewInject(R.id.tv_activity_repairer_detail_years)
    private TextView tvServeYears;
    @ViewInject(R.id.tv_activity_repairer_detail_certificates)
    private TextView tvCertificates;
    @ViewInject(R.id.tv_activity_repairer_detail_workcity)
    private TextView tvWorkCity;
    @ViewInject(R.id.lv_activity_repairer_detail_comment)
    private ListView lvComments;
    MasterManager masterManager=new MasterManager();                //使用此类进行网络数据请求
    private int repaireId;
    private ArrayList<ImageView> stars=new ArrayList<>();
    private RepairerCommentLvAdapter commentLvAdapter;              //评论listview的adapter
    private ArrayList<RepairerCommentEntity> comments=new ArrayList<>();    //评论数据
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair_detail);
        ViewUtils.inject(this);
        init();
        loadDetail();                                   //加载师傅详情
        loadComments();                                 //加载师傅评价
    }



    private void init() {
        repaireId=getIntent().getIntExtra("repairerId",0);//获得师傅id
        stars.add(ivStar1);                                 //将星星都加到arraylist里面，便于使用for循环来改变星星的图片
        stars.add(ivStar2);
        stars.add(ivStar3);
        stars.add(ivStar4);
        stars.add(ivStar5);
        commentLvAdapter=new RepairerCommentLvAdapter(comments,this);
        lvComments.setAdapter(commentLvAdapter);
    }

    private void loadDetail() {
        HashMap<String,String> map=new HashMap<>();
        map.put("repairerId", repaireId + "");
        masterManager.getMasterDetail(map, new ICallBack() {
            @Override
            public void onSuccess(String string) {
                MasterDetailEntity entity = JSONObject.parseObject(string, MasterDetailEntity.class);
                if (entity.getRc() == 0) {
                    repairerName.setText(entity.getName());
                    repairerSkill.setText(entity.getField());
                    for (int i = 0; i < entity.getStar(); i++) {
                        stars.get(i).setImageResource(R.mipmap.order_star_small_selected2x);
                    }
                    tvServeTimes.setText(entity.getServiceNum() + "");
                    tvServeYears.setText(entity.getWorkTime() + "");
                    tvCertificates.setText(entity.getCertNum() + "");
                    tvWorkCity.setText(entity.getCity());
                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        });

    }
    private void loadComments() {
        HashMap<String,String> map=new HashMap<>();
        map.put("repairerId",repaireId+"");
        map.put("start",0+"");
        map.put("count", 10 + "");
        masterManager.getMasterComment(map, new ICallBack() {
            @Override
            public void onSuccess(String string) {
                RepairerCommentEntity entity = JSONObject.parseObject(string, RepairerCommentEntity.class);
                if (entity.getRc() == 0) {

                    commentLvAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        });
    }
    @OnClick({R.id.iv_repairer_detail_back_iv})
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.iv_repairer_detail_back_iv:
                finish();
                break;
        }
    }
    //在销毁activity的时候取消网络数据请求，减少流量消耗，这也正是volley框架的优点之一
    @Override
    protected void onDestroy() {
        super.onDestroy();
        masterManager.requestQueue.cancelAll(RepairOrderManager.class);
    }
}
